package com.video.list.player

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.MediaMetadata
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.ConcatenatingMediaSource
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.video.list.player.bean.VideoInfo

class VideoMainActivity : AppCompatActivity(){

    var mVideoPageAdapter: VideoPageFragmentAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video_main)

        mVideoPageAdapter = VideoPageFragmentAdapter(this)
        val viewPager = findViewById<ViewPager2>(R.id.view_page)
        viewPager.orientation = ViewPager2.ORIENTATION_VERTICAL
        viewPager.offscreenPageLimit = 1
        viewPager.adapter = mVideoPageAdapter
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                Log.i("jiao","onPageSelected = $position")
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }

        })

        //设置数据
        setData()
    }

    /**
     * 设置数据源
     */
    private fun setData() {
        val json = AssetsUtil.getJson(this, "video_list.json")
        val type = object : TypeToken<ArrayList<VideoInfo>>() {}.type
        val data = Gson().fromJson<ArrayList<VideoInfo>>(json, type)

        data.forEach{
            VideoManager.instance.addMedialSource(it.mp4Url)
            VideoManager.instance.initExoPlayer()
        }

        mVideoPageAdapter?.setNewData(data)
    }




}